package main

import (
	"encoding/json"
	"fmt"
)

// JSON 序列化
// func Marshal(v interface{}) ([]byte, error)
// tips: 字段如果不能被导出，那么该字段会被忽略

type Student struct {
	Name     string
	Age      byte
	birthday string // {"Name":"jack","Age":12}
}

func testStruct() {
	stu := Student{"jack", 12, "2202-11-2"}
	data, err := json.Marshal(stu)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(data)) // {"Name":"jack","Age":12}
}

func testMap() {
	m := make(map[string]interface{})
	m["a"] = "aa"
	m["b"] = "bb"
	m["arr"] = []string{"aa", "bb", "cc"}

	data, err := json.Marshal(m)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(data)) // {"a":"aa","arr":["aa","bb","cc"],"b":"bb"}
}

func testSlice() {
	var slice []map[string]interface{}

	m := make(map[string]interface{})
	m["a"] = "aa"
	m["b"] = "bb"
	slice = append(slice, m)

	m1 := make(map[string]interface{})
	m1["d"] = "dd"

	slice = append(slice, m1)

	data, err := json.Marshal(slice)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(data)) // [{"a":"aa","b":"bb"},{"d":"dd"}]

}
func main() {
	testStruct()
	testMap()
	testSlice()
}
